package com.zhangjinghao.entity;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;

/**
 * @Description  
 * @Author  zhangxingang
 * @Date 2022-03-28 
 */

@Data
@Accessors(chain = true)
@NoArgsConstructor
@Document(indexName = "food_index")
public class Food implements Serializable {


	/** 编号 */
	@Id
	private Integer id;

	/** 厂家 */
	private Vender vender;

	/** 名称 */
	@NotBlank(message = "所填项不能为空")
	private String name;

	/** 创建日期 */
	@NotNull(message = "所填项不能为空")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date createDate;

	/** 省id */
	private City province;

	/** 市id */
	private City town;

	/** 县id */
	private City county;

	/** 到期时间 */
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date expirationDate;

	/** 保质天数 */
	@NotNull(message = "所填项不能为空")
	private Integer expirationNum;

	/** 剩余天数 */
	private long residueNum;

	/** 消息 */
	private String message;

	public long getResidueNum() {

		if (expirationDate==null||createDate==null){
			return 0;
		}
		return DateUtil.between(new Date(),expirationDate, DateUnit.DAY);
	}

	public Food(Integer id, String name, long residueNum) {
		this.id = id;
		this.name = name;
		this.residueNum = residueNum;
	}
}
